<template>
  <div>
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="120px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="接口名称" prop="apiName">
        <el-input v-model="dataForm.apiName" placeholder="接口名称" />
      </el-form-item>
      <el-form-item label="接口url" prop="apiUrl">
        <el-input v-model="dataForm.apiUrl" placeholder="接口url" />
      </el-form-item>
      <el-form-item label="接口描述" prop="apiDesc">
        <el-input v-model="dataForm.apiDesc" placeholder="接口描述" />
      </el-form-item>
      <el-form-item label="接口类型" prop="apiType">
        <!-- <el-input v-model="dataForm.apiType" placeholder="接口类型"></el-input> -->
        <el-select v-model="dataForm.apiType" placeholder="请选择">
          <el-option
            v-for="item in apiTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <!-- <el-input v-model="dataForm.enabled" placeholder="是否启用"></el-input> -->
        <el-select v-model="dataForm.enabled" placeholder="请选择">
          <el-option
            v-for="item in enabledList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接口执行类名" prop="executeClass">
        <el-input v-model="dataForm.executeClass" placeholder="接口执行类名" />
      </el-form-item>
      <el-form-item label="接口执行方法" prop="executeMethod">
        <el-input v-model="dataForm.executeMethod" placeholder="接口执行方法" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        apiName: "",
        apiUrl: "",
        apiDesc: "",
        apiType: "",
        enabled: "",
        createTime: "",
        executeClass: "",
        executeMethod: ""
      },
      dataRule: {
        apiName: [
          { required: true, message: "接口名称不能为空", trigger: "blur" }
        ],
        apiUrl: [
          { required: true, message: "接口url不能为空", trigger: "blur" }
        ],
        apiType: [
          { required: true, message: "接口类型不能为空", trigger: "blur" }
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ]
      },
      apiTypeList: [
        {
          label: "内部接口",
          value: 0
        },
        {
          label: "外部接口",
          value: 1
        }
      ],
      enabledList: [
        {
          label: "启用",
          value: 1
        },
        {
          label: "禁用",
          value: 0
        }
      ]
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/sysapiconfig/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.apiName = data.sysApiConfig.apiName;
              this.dataForm.apiUrl = data.sysApiConfig.apiUrl;
              this.dataForm.apiDesc = data.sysApiConfig.apiDesc;
              this.dataForm.apiType = data.sysApiConfig.apiType;
              this.dataForm.enabled = data.sysApiConfig.enabled;
              this.dataForm.createTime = data.sysApiConfig.createTime;
              this.dataForm.executeClass = data.sysApiConfig.executeClass;
              this.dataForm.executeMethod = data.sysApiConfig.executeMethod;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/sysapiconfig/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              apiName: this.dataForm.apiName,
              apiUrl: this.dataForm.apiUrl,
              apiDesc: this.dataForm.apiDesc,
              apiType: this.dataForm.apiType,
              enabled: this.dataForm.enabled,
              createTime: this.dataForm.createTime,
              executeClass: this.dataForm.executeClass,
              executeMethod: this.dataForm.executeMethod
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
